﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Sac.Web;

public partial class Admin_PowerManage : System.Web.UI.Page
{
    MenuTreeOperate rootClass = new MenuTreeOperate();
    RolePowerOperate role = new RolePowerOperate();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CreateTreeMenu();
            try
            {
                string roleId = Request.QueryString[0].ToString();
                RolePower roleInfo = new RolePower();
                roleInfo = role.GetRoleById(roleId);
                lblRoleId.Text = roleInfo.IdKey.ToString();
                lblRoleName.Text = roleInfo.RoleName;
                string powerCode = roleInfo.PowerCode;
                if (powerCode != "")
                {
                    CheckTreeView(powerCode, treeMenuPower.Nodes);
                }
            }
            catch
            {
               
            }
        }
    }

    /// <summary>
    /// 创建菜单树
    /// </summary>
    private void CreateTreeMenu()
    {
        DataTable dtMenuList = rootClass.GetAllMenuNode();
        treeMenuPower.Nodes.Clear();
        InitTree(treeMenuPower.Nodes, "-1", dtMenuList);
        //treeMenuPower.Attributes.Add("onclick", "postBackByObject()");
    }

    /// <summary>
    /// 初始化菜单树
    /// </summary>
    /// <param name="treeNodes"></param>
    /// <param name="fartherId"></param>
    /// <param name="dtAllMenuNode"></param>
    protected void InitTree(TreeNodeCollection treeNodes, string fartherId, DataTable dtAllMenuNode)
    {
        DataView dv = new DataView();
        dv.Table = dtAllMenuNode;
        dv.RowFilter = "fartherid=" + fartherId;
        foreach (DataRowView drv in dv)
        {
            TreeNode node = new TreeNode();
            node.Value = drv["ID_KEY"].ToString();
            node.Text = drv["MENU_NAME"].ToString();
            node.SelectAction = TreeNodeSelectAction.Expand;
            treeNodes.Add(node);
            InitTree(node.ChildNodes, node.Value, dtAllMenuNode);
        }
    }


    protected void treeMenuPower_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
    {

    }

    /// <summary>
    /// 根据权限码选中菜单节点
    /// </summary>
    /// <param name="powerCode">权限码</param>
    /// <param name="treeNodes"></param>
    private void CheckTreeView(string powerCode, TreeNodeCollection treeNodes)
    {
        foreach (TreeNode node in treeNodes)
        {
            int index = powerCode.IndexOf(node.Value + "|");
            if (index != -1)
            {
                node.Checked = true;
            }
            CheckTreeView(powerCode, node.ChildNodes);
        }
    }

    /// <summary>
    /// 清除所有选择的菜单节点
    /// </summary>
    /// <param name="treeNodes"></param>
    private void ClearTreeCheck(TreeNodeCollection treeNodes)
    {
        foreach (TreeNode node in treeNodes)
        {
            if (node.Checked)
            {
                node.Checked = false;
            }
            ClearTreeCheck(node.ChildNodes);
        }
    }

    /// <summary>
    /// 根据菜单分配情况获取新的权限码
    /// </summary>
    /// <param name="treeNodes"></param>
    /// <param name="powerCode"></param>
    private void GetPowerCode(TreeNodeCollection treeNodes,ref string powerCode)
    {
        foreach (TreeNode node in treeNodes)
        {
            if (node.Checked)
            {
                powerCode += node.Value + "|";
            }
            GetPowerCode(node.ChildNodes,ref powerCode);
        }
    }

    protected void btnSure_Click(object sender, EventArgs e)
    {
        string  message = "";
        if (lblRoleId.Text != "")
        {
            string powerCode = "";
            GetPowerCode(treeMenuPower.Nodes, ref powerCode);
            if (role.UpdateRolePowerById(lblRoleId.Text, powerCode))
            {
                message = "权限分配成功！";
            }
            else
            {
                message = "权限分配失败！";
            }
        }
        else
        {
            message = "请先选中角色，再进行权限分配！";
        }
        Response.Write("<script>alert('" + message + "')</script>");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ClearTreeCheck(treeMenuPower.Nodes);
        lblRoleId.Text = "";
        lblRoleName.Text = "尚未选择任何角色";
    }
}
